import request from '@/utils/request'
import qs from 'qs'

// 登录请求方法
const loginreq = (method, url, data) => {
    return request({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        data: qs.stringify(data),
        traditional: true,
    }).then(res => res.data);
};

// 通用公用方法
const reqJson = (method, url, params = {}) => {
    let requestData = {}
    // 如果有参数，则序列化为JSON字符串；否则data字段为空
    if (Object.keys(params).length !== 0) {
        requestData.data = JSON.stringify(params);
    }

    return request({
        method: method,
        url: url,
        headers: {
            'Content-Type': 'application/json'
        },
        ...requestData,
        traditional: true,
    }).then(res => res.data);
};

// 通用公用方法
const req = (method, url, data) => {
    return request({
        method: method,
        url: url+'?'+qs.stringify(data)
    }).then(res => res.data);
};


export {
    loginreq,
    req,
    reqJson,
    request
}